/*******************************************************************
This file is part of iLaunch.

iLaunch is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

iLaunch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with iLaunch.  If not, see <http://www.gnu.org/licenses/>.
*******************************************************************/

#include "stdafx.h"
#include <initguid.h>

#include "sOffice.h"
//#include "PhoneUtils.h"
#include "FileUtils.h"
#include "FindFileSorted.h"

#include "GraphicFunctions.h"

// *************************************************
// These functions are for the main list of appointments
// *************************************************

HRESULT sOfficePopulate(DataItem * parent, void (*adder)(DataItem*),
					 CSettings * pSettings) {

	HRESULT hr = S_OK;
	//DWORD dwNumberOfBytesWritten = 0;
	
	TCHAR szNamePath[MAX_PATH] = {0};	//path to be used to get the name without .lnk
	TCHAR szLinkPath[MAX_PATH] = {0};	//path to be used to get the full link
	TCHAR szIconLink[SECONDARY_TEXT_LENGTH] = {0};
	StringCchCopy(szIconLink, SECONDARY_TEXT_LENGTH, TEXT("icon.lnk"));

	// This will hold our "next" list item, which we'll later
	// pass to this->_addListItem
	DataItem data = {0};
	data.iGroup = 0;
	data.ID = 1;
	data.type = diListItem;

	// localized start menu/programs folder
	// localized folder name (from language file)
	SHGetSpecialFolderPath(NULL, szNamePath, CSIDL_PROGRAMS, false);
	StringCchCat(szNamePath, MAX_PATH, TEXT("\\"));
	StringCchCat(szNamePath, MAX_PATH, pSettings->office_string);
	StringCchCat(szNamePath, MAX_PATH, TEXT("\\"));

   CFindFileSorted *findFileSorted;

   findFileSorted = new CFindFileSorted(szNamePath, _T("*.lnk"));

   for (int i = 0; i < findFileSorted->getCount(); i++)
   {
	   StringCchCopy(data.szPrimaryText, PRIMARY_TEXT_LENGTH, findFileSorted->getAt(i));

		// this is the grouping: first char of szprimarytext
		//data.iGroup = (int)_totupper(data.szPrimaryText[0]);
		//data.iGroup = 1;

		bool bIsIconLink = false;
		if(CompareString(LOCALE_NEUTRAL, NORM_IGNORECASE, data.szPrimaryText, -1, szIconLink, -1) == CSTR_EQUAL)
			bIsIconLink = true;

		data.szPrimaryText[_tcslen(data.szPrimaryText) - 4] = '\0';

		// see above - bad coding, oh well...
		SHGetSpecialFolderPath(NULL, szLinkPath, CSIDL_PROGRAMS, false);
		StringCchCat(szLinkPath, MAX_PATH, TEXT("\\"));
		StringCchCat(szLinkPath, MAX_PATH, pSettings->office_string);
		StringCchCat(szLinkPath, MAX_PATH, TEXT("\\"));

		StringCchCat(szLinkPath, MAX_PATH, findFileSorted->getAt(i));
		StringCchCopy(data.szLinkPath, MAX_PATH, szLinkPath);

		if(!bIsIconLink)
			adder(&data);
	}

	hr = S_OK;

//Error:
//	SysFreeString(bstrPrimary);
	return hr;
}

HRESULT sOfficeClick(DataItem * data, float x,  
				  int * newScreen, CSettings * pSettings) {

	SHELLEXECUTEINFO sei = {0};

	sei.cbSize = sizeof(sei);
	sei.lpVerb = TEXT("Open");
	sei.lpFile = data->szLinkPath;
	sei.lpParameters = TEXT("");
	sei.nShow = SW_SHOW;
	ShellExecuteEx(&sei);

	return S_OK;
}

HRESULT sOfficeGetTitle(DataItem * parent, TCHAR * buffer, int cchDest,
					 CSettings * pSettings) {

						 StringCchCopy(buffer, cchDest, parent->szPrimaryText);
						 return S_OK;
}

HRESULT sOfficeGetGroup(DataItem * data, TCHAR * buffer, int cchDest,
					 CSettings * pSettings) {

	buffer[0] = (TCHAR)data->iGroup;
	buffer[1] = 0;
	return S_OK;
}

//allelimo TODO: DELETE THIS FUNCTION - no bitmaps in appointments

// **************************************************************************
// Function Name: PoomDetailsLoadBitmap
// 
// Purpose: given a DataItem pointer, finds an associated bitmap 
// and its dimensions
//
// Arguments:
//    IN  int      index    - index of the contact
//    IN  HBITMAP* phBitmap - bitmap of the contact's picture
//    OUT UINT*    puWidth  - width of the bitmap
//    OUT UINT*    puHeight - height of the bitmap
//
// Return Values:
//    HRESULT - S_OK if success, failure code if not
//

//HRESULT PoomDetailsLoadBitmap(DataItem * data, HBITMAP * phBitmap, 
//							  UINT * puWidth, UINT * puHeight) {
//
//								  HRESULT   hr;
//								  IItem*    pItem = NULL;
//								  IStream*  pStream = NULL;
//								  ULONG     ulSize;
//
//								  // Make sure we can access POOM
//								  hr = initPoom();
//								  CHR(hr);
//
//								  hr = polApp->GetItemFromOidEx(data->oId, 0, &pItem);
//								  CHR(hr);
//
//								  // Extract the picture from the contact
//								  hr = pItem->OpenProperty(PIMPR_PICTURE, GENERIC_READ, &pStream);
//								  CHR(hr);
//
//								  hr = GetStreamSize(pStream, &ulSize);
//								  CHR(hr);
//
//								  // In some cases, the property may exist even if there is no picture.
//								  // Make sure we can access the stream and don't have a 0 byte stream
//								  CBR(ulSize > 0);
//
//								  hr = GetBitmapFromStream(pStream, phBitmap, puWidth, puHeight);
//								  CHR(hr);
//
//Error:
//								  RELEASE_OBJ(pItem);
//								  RELEASE_OBJ(pStream);
//
//								  return hr;
//}


HRESULT FakeAddItem() {
	HRESULT hr = S_OK;

	return hr;
}
